package company.permissionsystem.security.dao;


import company.permissionsystem.security.SystemUserDetails;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 *  建表规则(必须拥有的字段, ID为主键)：
 *      sys_user(id, status)
 *      sys_user_role(user_id, role_id)
 *      sys_role(id, role_name)
 *      sys_role_permission(role_id, permission_id)
 *      sys_permission(id, url)  URL(Controller层) : @PreAuthorize("hasAuthority('user:edit')")
 * */
public interface UserDetailsDao {

    /**
     *  通过用户名查找用户名、密码、用户状态
     **/
    @Select("select username,password,status from sys_user where username=#{username}")
    SystemUserDetails findUserByName(String username);
    /**
     *  通过用户名查询对应角色
     * */
    @Select("select role_name from sys_role where id = " +
            "(select role_id from sys_user_role where user_id=\n" +
            "(select id from sys_user where username=#{username}))")
    String findRoleNameByUserName(String username);

    /**
     *  通过用户名查询对应角色下所有权限资源
     * */
    @Select("select url from sys_permission where id in " +
            "(select permission_id from sys_role_permission where role_id=" +
            "(select id from sys_role where role_name=" +
            "(select role_name from sys_role where id=" +
            "(select role_id from sys_user_role where user_id=\n" +
            "(select id from sys_user where username=#{username})))))")
    List<String> findAuthortyByUserName(String username);

}
